package com.donnie.shopping.service.impl;

import com.donnie.shopping.config.FruitConfig;
import com.donnie.shopping.constants.FruitConst;
import com.donnie.shopping.entity.vo.FruitOrderItem;
import com.donnie.shopping.service.ICalculationStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * @description: 满减计价策略
 * @author: ChenDeXin
 * @time: 2025/5/20 12:33
 */
@Slf4j
@Service("reductionCalculator")
public class ReductionCalculator implements ICalculationStrategy {
    @Resource
    FruitConfig fruitConfig;

    @Resource
    Map<String, ICalculationStrategy> calculatinoMap;

    ICalculationStrategy calculationStrategy;

    /**
     * 计算费用 <br>
     *
     * @param orderItems
     * @return java.lang.Double <br>
     * @date: 2025/5/20 10:05 <br>
     * @author: ChenDeXin <br>
     */
    @Override
    public Double calculate(List<FruitOrderItem> orderItems) {
        //TODO 扩展基于哪种基础的计价策略，再进行满减
        this.calculationStrategy = calculatinoMap.get(fruitConfig.getBaseCalStrategy());
        Double amount = calculationStrategy.calculate(orderItems);

        Double threshold = fruitConfig.getReductionList().get(FruitConst.REDUCTION_THRESHOLD_KEY);
        Double reduction = fruitConfig.getReductionList().get(FruitConst.REDUCTION_REDUCTION_KEY);

        //按照题设，满减策略，每满100减10块
        if(amount >= threshold) {
            int reductionTimes = (int) (amount / threshold);
            Double reductionAmt = new BigDecimal(reductionTimes * reduction)
                    .setScale(3, RoundingMode.HALF_UP).doubleValue();

            log.info("已满足100 减 10 块！！原价：{}, 共减：{}, 满减后的价钱：{}", amount, reductionAmt, amount - reductionAmt);
            return amount - reductionAmt;
        }

        log.info("不满足100 减 10 块！！原价：{}", amount);
        return amount;
    }
}
